﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockInHeaderEdit : Form
    {
        DataTable dtSupplier;
        private StockInHeader stockInHeaderBLL;
        private Supplier supplierBLL;
        private StockInType stockInTypeBLL;

        public frmStockInHeaderEdit()
        {
            InitializeComponent();
            dtSupplier = new DataTable();

            stockInHeaderBLL = new StockInHeader();
            stockInHeaderBLL.StockInTypeName = new StockInType();
            stockInHeaderBLL.SupplierName = new Supplier();

            supplierBLL = new Supplier();
            stockInTypeBLL = new StockInType();
        }

        private void frmStockInHeaderEdit_Load(object sender, EventArgs e)
        {
            BindSupplier();
            
        }

        /// <summary>
        /// 接收header并显示
        /// </summary>
        /// <param name="header"></param>
        public void Init(StockInHeader header)
        {
            BindStockInType();
            stockInHeaderBLL = header;
            SetInfo();
        }

        /// <summary>
        /// 设置信息
        /// </summary>
        public void SetInfo()
        {
            txtStockInNum.Text = stockInHeaderBLL.StockInNum;
            txtSupplier.Text = stockInHeaderBLL.SupplierName.SupplierName;
            cbxStockInType.SelectedItem = stockInHeaderBLL.StockInTypeName.StockInTypeName;
            dtpStockInDate.Value = stockInHeaderBLL.StockInDatetime;
            rtbRemark.Text = stockInHeaderBLL.Remark;
            this.dgvSupplier.Visible = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            stockInHeaderBLL.SetStockInHeader(GetObject());
            this.DialogResult = DialogResult.OK;
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            SetInfo();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtSupplier.Text = "";
            rtbRemark.Text = "";
            dtpStockInDate.Value = DateTime.Now;
            cbxStockInType.SelectedIndex = 0;
            this.dgvSupplier.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 供应商信息智能录入

        private void BindSupplier()
        {
            dgvSupplier.DataSource = dtSupplier = supplierBLL.GetAllSupplier();
        }


        private void txtSupplier_TextChanged(object sender, EventArgs e)
        {
            this.Check();

            int times = 0;

            this.dgvSupplier.Top = this.txtSupplier.Top + this.txtSupplier.Height;

            if (times == 0)
            {
                times = 1;
                BindSupplier();
            }

            if (txtSupplier.Text == "")
            {
                if (dtSupplier.DefaultView.Count != 0)
                    dgvSupplier.Visible = true;
            }

            dtSupplier.DefaultView.RowFilter = "supplier_code like '" + txtSupplier.Text.Trim() + "%'";

            if (dtSupplier.DefaultView.Count != 0)
            {
                dgvSupplier.Visible = true;
                dgvSupplier.CurrentCell = dgvSupplier[0, 0];
            }
            else
                dgvSupplier.Visible = false;
        }

        private void dgvSupplier_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvSupplier.Rows.Count == 0)
                    return;

                dgvSupplier.Focus();
                if (dgvSupplier.CurrentRow.Index + 1 < dgvSupplier.Rows.Count)
                {
                    dgvSupplier.CurrentCell = dgvSupplier[0, dgvSupplier.CurrentRow.Index + 1];
                }
            }


            if (e.KeyCode == Keys.Enter)
            {
                txtSupplier.Text = dgvSupplier.CurrentRow.Cells[1].Value.ToString();
                dgvSupplier.Visible = false;
            }
        }

        private void txtSupplier_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvSupplier.Rows.Count == 0)
                    return;

                dgvSupplier.Focus();
                if (dgvSupplier.CurrentRow.Index + 1 < dgvSupplier.Rows.Count)
                {
                    dgvSupplier.CurrentCell = dgvSupplier[0, dgvSupplier.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                if (dgvSupplier.Visible == true)
                {
                    txtSupplier.Text = dgvSupplier.CurrentRow.Cells[1].Value.ToString();
                    dgvSupplier.Visible = false;
                }
            }
        }

        #endregion

        /// <summary>
        /// 获取header
        /// </summary>
        /// <returns></returns>
        public StockInHeader GetObject()
        {
            StockInHeader header = new StockInHeader();
            header.StockInTypeName = new StockInType();
            header.SupplierName = new Supplier();

            header.StockInNum = txtStockInNum.Text.Trim();
            header.StockInTypeName.StockInTypeName = cbxStockInType.Text;
            header.StockInDatetime = dtpStockInDate.Value;
            header.SupplierName.SupplierName = txtSupplier.Text.Trim();
            header.CreatedBy = frmMain.userObject.UserName; 
            header.ApprovalBy = "";  
            header.Remark = rtbRemark.Text;
            header.Status = "未审核";

            return header;
        }

        /// <summary>
        /// 根据必填信息是否完整来设置按钮可用状态
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            if (txtSupplier.Text.Trim() != "")
                return btnUpdate.Enabled = true;
            else
                return btnUpdate.Enabled = false;
        }

        /// <summary>
        /// 绑定入库类型
        /// </summary>
        private void BindStockInType()
        {
            IList<StockInType> list = stockInTypeBLL.GetAllStockInType();
            foreach (StockInType obj in list)
            {
                cbxStockInType.Items.Add(obj.StockInTypeName);
            }
            //cbxStockInType.SelectedIndex = 0;
        }

      
    }
}